﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;

namespace UILibrary
{
    public class CommandBar
    {
        private global::System.Object missing = global::System.Type.Missing;

        Office.CommandBarButton m_showMenu;
        Office.CommandBarButton m_updateMenu;
        Office.CommandBarButton m_aboutMenu;
        private string m_topMenuTag = "Office in Cloud#3131";
        private string m_showTag = "Office in Cloud#3132";
        private string m_updateTag = "Office in Cloud#3133";
        private string m_aboutTag = "Office in Cloud#3134";

        private Office.CommandBarButton addMenuItem(Office.CommandBarPopup parent, string title)
        {
            Office.CommandBarButton menuCommand = (Office.CommandBarButton)parent.Controls.Add(Office.MsoControlType.msoControlButton,
                                                                          missing,
                                                                          missing,
                                                                          missing,
                                                                          true);
            menuCommand.Caption = "&" + title;

            return menuCommand;
        }

        public void removeBar(Office.CommandBar activeMenuBar)
        {
            try
            {
                Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
                    activeMenuBar.FindControl(Office.MsoControlType.msoControlPopup, 
                                                System.Type.Missing,
                                                m_topMenuTag, 
                                                true, 
                                                true);

                if (foundMenu != null)
                {
                    foundMenu.Delete(true);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void buildMenuBar(Office.CommandBar activeMenuBar, 
                                    Office._CommandBarButtonEvents_ClickEventHandler showActionPane,
                                    Office._CommandBarButtonEvents_ClickEventHandler checkForUpdates,
                                    Office._CommandBarButtonEvents_ClickEventHandler showAbout)
        {
            try
            {
                //in word 2003 even temporary menu still exist
                removeBar(activeMenuBar);

                Office.CommandBarPopup popupMenu = (Office.CommandBarPopup)activeMenuBar.Controls.Add(Office.MsoControlType.msoControlPopup,
                                                                              missing,
                                                                              missing,
                                                                              activeMenuBar.Controls.Count,
                                                                              true);
                if (popupMenu != null)
                {
                    popupMenu.Caption = "&" + Properties.Resources.AppName;
                    popupMenu.Tag = m_topMenuTag;

                    m_showMenu = addMenuItem(popupMenu, Properties.Resources.ShowMenuName);
                    m_updateMenu = addMenuItem(popupMenu, Properties.Resources.UpdateMenuName);
                    m_aboutMenu = addMenuItem(popupMenu, Properties.Resources.AboutMenuName);

                    m_showMenu.Tag = m_showTag;
                    m_updateMenu.Tag = m_updateTag;
                    m_aboutMenu.Tag = m_aboutTag;

                    m_showMenu.Click += showActionPane;
                    m_updateMenu.Click += checkForUpdates;
                    m_aboutMenu.Click += showAbout;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
       }
}
